package mammuthus.template.hbase

import mammuthus.template.TemplateProcessor
import mammuthus.master.Master
import mammuthus.protocal.WriteFileTaskResponse
import mammuthus.tool.TemplateTool

/**
 * 6/8/15 WilliamZhu(allwefantasy@gmail.com)
 */
class HbaseSiteProcessor(master: Master) extends TemplateProcessor {
  override def process(fileName: String, targetFilePath: String, params: Map[String, AnyRef]): List[WriteFileTaskResponse] = {
    val appName = params("appName").asInstanceOf[String]
    val app = params.getOrElse("app", appName)

    val zookeeper = params.getOrElse("zookeeper","zookeeper").asInstanceOf[String]
    val zkHosts = master.appInstallHosts(zookeeper)

    val hadoopName = params.getOrElse("hadoopName","hadoop").asInstanceOf[String]

    val hadoopNameNode = master.appGetExtra(Map("appName"->hadoopName,"name"->"nameNodes")).toList(0)("extra")

    val hadoopInstallDir = master.appGetExtra(Map("appName"->hadoopName,"name"->"installPath")).toList(0)("extra")

    val coreSite = master.appLog(hadoopNameNode, s"${hadoopInstallDir}/${hadoopName}/etc/hadoop/hdfs-site.xml", -1, -1)
    val coreSiteMsg = coreSite.map(f=>f.msg).toList(0)

    val hostNames = params.getOrElse("slaves","").asInstanceOf[String]

    master.writeFile(hostNames, coreSiteMsg, targetFilePath.replace("hbase-site","hdfs-site"), false)

    val dfsNameservices = params.getOrElse("dfs_nameservices", "mammuthus")

    val templateParams = Map(
      "dfsNameservices" -> dfsNameservices,
      "hbase_zookeeper_nodes" -> zkHosts
    )
    val configStr = TemplateTool.render(s"/template/app/$app/$fileName", templateParams)

    master.writeFile(hostNames, configStr, targetFilePath, false)
  }

}
